import { Component, OnInit, Injector, ViewChild, EventEmitter, Output } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';
import {
    StockZT_TransfersBillItemServiceProxy,
    GetStock_AuditInputDto,
    Stock_BillListDto,
    StockZT_ToolTransferServiceProxy,
    Stock_ToolingTransferBaseEditDto
} from '@shared/service-proxies/stock-service-proxies';
import * as _ from 'lodash';
import { ModalDirective } from 'ngx-bootstrap';
@Component({
    templateUrl: './create-or-edit-tooling-transfer-opinion-modal.component.html',
    animations: [appModuleAnimation()],
    selector: 'createToolingTransferOpinionModalComponent'
})
export class CreateToolingTransferOpinionModalComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    @ViewChild('detailDataTable') detailDataTable: Table;
    @ViewChild('detailPaginator') detailPaginator: Paginator;
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Output() modalUpdate: EventEmitter<any> = new EventEmitter<any>();
    auditDto = new GetStock_AuditInputDto();
    selectedBill: Stock_BillListDto;
    billbase=new Stock_ToolingTransferBaseEditDto();
    optionOutAccounting;
    optionOutLeader
    index;
    f_OutAccountId;
    f_OutLeaderId
    constructor(injector: Injector, private _billService: StockZT_ToolTransferServiceProxy,
        private _tranbillService: StockZT_TransfersBillItemServiceProxy) {
        super(injector);
    }
    active = false;
    saving = false;
    isaudit = false;
    ngOnInit() {
    }

    //新增
    show(selectedBill, index): void {
        this.active = true;
        this.selectedBill = selectedBill;
        this.index = index;
        this.isaudit= false;
        //调出单位会计
        this._tranbillService.getRoleUser('车间财务', this.selectedBill.f_ToDeptId).subscribe(result => {
            this.optionOutAccounting = result.items.map(e => {
                return {
                    label: e.displayText,
                    value: e
                };
            });
        });
        //调出单位领导
        this._tranbillService.getRoleUser('车间主任', this.selectedBill.f_ToDeptId).subscribe(result => {
            this.optionOutLeader = result.items.map(e => {
                return {
                    label: e.displayText,
                    value: e
                };
            });
        });
        this._billService.getNodeIndexByWorkflowId(this.selectedBill.f_WorkflowId).subscribe(result => {
            if (result == 3 && this.index == '0') {
                this.isaudit = true;
            }
        });
        this.modal.show();
    }
    close(): void {
        this.active = false;
        this.modal.hide();
    }
    //保存
    save(): void {
        this.auditDto.id = this.selectedBill.id;
        this.billbase.id=this.selectedBill.id;
        this.primengTableHelper.showLoadingIndicator();
        if (this.index == '0') {
            this.auditDto.f_Status = '同意';
            this._billService.approveToolingAuditBill(this.auditDto).subscribe(() => {
                // if (this.isaudit) {
                //     if(this.f_OutAccountId == undefined){
                //         this.message.warn("请选择调出单位会计");
                //         return;
                //     }
                //     if(this.f_OutLeaderId == undefined){
                //         this.message.warn("请选择调出单位领导");
                //         return;
                //     }
                //     this.billbase.f_OutAccountId=this.f_OutAccountId.value;
                //     this.billbase.f_OutLeaderId=this.f_OutLeaderId.value;
                //     this.billbase.f_OutAccount=this.f_OutAccountId.displayText;
                //     this.billbase.f_OutLeader=this.f_OutLeaderId.displayText;
                //     this._billService.creatWorkflowOfToolingAfterAudit(this.billbase).subscribe(() => {
                //         this.notify.success(this.l('审核成功'));
                //         this.auditDto=new GetStock_AuditInputDto();
                //         this.billbase=new Stock_ToolingTransferBaseEditDto();
                //         this.modalSave.emit();
                //         this.close();
                //         this.primengTableHelper.hideLoadingIndicator();
                //     });
                // }else{
                    this.notify.success(this.l('审核成功'));
                    this.auditDto=new GetStock_AuditInputDto();
                    this.billbase=new Stock_ToolingTransferBaseEditDto();
                    this.modalSave.emit();
                    this.close();
                    this.primengTableHelper.hideLoadingIndicator();
                // }
            });
        }
        if (this.index == '1') {
            this.auditDto.f_Status = '退回';
            this._billService.sendBackToolingAuditBill(this.auditDto).subscribe(() => {
                this.notify.success(this.l('退回成功'));
                this.modalSave.emit();
                this.close();
                this.primengTableHelper.hideLoadingIndicator();
            });
        }
    }
}
